if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            "user": get_cookie("user"),
            "key": get_cookie("key")
        },
        success: function (_res) {
            res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
                $(".content_left>ul").empty();
                var url=location.href;
                var tiao=true;
                $.each(res.data[0].power_list,function (i,o) {
                    if(url.indexOf(o.url)!==-1){
                        tiao=false;
                    }
                    var _background_=  url.indexOf(o.url)!==-1?'style="background:#000;"':"";

                    $(".content_left>ul").append('<a href="'+o.url+'"> <li '+_background_+'><span class="glyphicon '+o.icon+'"></span><p>'+o.names+'</p></li></a>');

                });

                if(tiao){
                    location.href=res.data[0].power_list[0].url;
                }
            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }

        },
        error: function () {
            location.href = key_address;
        }
    });
} else {
    location.href = key_address;
}
$(function () {
    var houtai = {
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        linshi_search: {
            class1: "",
            class2: "",
            state: "",
            search: ""
        },
        linshi_id: null,
        linshi_arrid: [],
        queren_type: null,//0删除
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        jiancha: function (n) {
            // 判断是够为空
            for (var i = 0; i < $(".add_goods_body .input_is:visible").length; i++) {
                if ($(".add_goods_body .input_is:visible").eq(i).val() === "") {
                    $(".add_goods_body .input_is:visible").eq(i).focus();
                    houtai.hint("请填写 " + $(".add_goods_body .input_is:visible").eq(i).prev().text());
                    return false;
                }
            }
            // 判断是否上传默认图片
            if ($("#input_file_main_add")[0].files.length === 0 && n === 1) {
                houtai.hint("请上传轮播图片");
                return false;
            }
            return true;
        },
        shop_list_ajax: function () {

            $.ajax({
                url: ip + "index.php?s=admincms/silde/show",
                type: "post",
                data: {
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        houtai.table_string(res);
                    }

                },
                error:function () {
                    $("#hint_text").text("列表请求失败！！");
                }
            });
        },
        table_string: function (res) {
            // 写入table
            var string_head = "<thead><tr><th>ID</th><th>名称</th><th>介绍 </th ><th>操作</th></tr></thead>";
            var string_body = "";
            if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    string_body += '<tr>' +
                        '<td style="vertical-align: middle" class="fenlei_id text-center">' + o.id + '</td>' +
                        '<td style="vertical-align: middle" class="fenlei_name">' + o.sort + '</td>' +
                        '<td style="vertical-align: middle" class="fenlei_jieshao">' + o.url + '</td>' +
                        '<td style="vertical-align: middle;width: 200px;" class="fenlei_controls text-center"> <button type="button" class="btn btn-warning btn-xs fenlei_set"> <span class="glyphicon glyphicon-tags" aria-hidden="true"></span> 修改</button> <button type="button" class="btn btn-danger btn-xs fenlei_remove"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除</button> <button type="button" class="btn btn-info btn-xs anli_chakan"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 查看案例</button></td>' +
                        '</tr>';
                });
            }else{
                string_body = "<tr class='text-center'><td colspan='13'>没有数据</td></tr>";
            }

            $("table").empty().append(string_head + string_body);
        },
        anli_list_ajax: function () {

            $.ajax({
                url: ip + "index.php?s=admincms/silde/show",
                type: "post",
                data: {
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        houtai.anli_table_string(res);
                    }

                },
                error:function () {
                    $("#hint_text").text("列表请求失败！！");
                }
            });
        },
        anli_table_string: function (res) {
            // 写入table
            var string_head = "<thead><tr><th>ID</th><th>名称</th><th>介绍 </th ><th>操作</th></tr></thead>";
            var string_body = "";
            if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    string_body += '<tr>' +
                        '<td style="vertical-align: middle" class="fenlei_id text-center">' + o.id + '</td>' +
                        '<td style="vertical-align: middle" class="fenlei_name">' + o.sort + '</td>' +
                        '<td style="vertical-align: middle" class="fenlei_jieshao">' + o.url + '</td>' +
                        '<td style="vertical-align: middle;width: 200px;" class="fenlei_controls text-center"> <button type="button" class="btn btn-warning btn-xs fenlei_set"> <span class="glyphicon glyphicon-tags" aria-hidden="true"></span> 修改</button> <button type="button" class="btn btn-danger btn-xs fenlei_remove"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除</button> <button type="button" class="btn btn-info btn-xs anli_chakan"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 查看案例</button></td>' +
                        '</tr>';
                });
            }else{
                string_body = "<tr class='text-center'><td colspan='13'>没有数据</td></tr>";
            }

            $("#anli_table").empty().append(string_head + string_body);
        },
        huoqu_url_add: function (file) {
            var form_reader = new FileReader();
            form_reader.onload = function (e) {
                $(".img_wrap_main_add").append("<img src='" + e.target.result + "'/>");
            };
            form_reader.readAsDataURL(file);
        },
        huoqu_url_set: function (file) {
            var form_reader = new FileReader();
            form_reader.onload = function (e) {
                $(".img_wrap_main_set").append("<img src='" + e.target.result + "'/>");
            };
            form_reader.readAsDataURL(file);
            console.log(file);
        },
        tianjia: function (n) {
            if (this.jiancha(n)) {
                $(".loading").fadeIn(200);
                var form_data = new FormData();
                form_data.append("silde_img", $("#input_file_main_add")[0].files[0]);
                form_data.append("sort", $("#add_xuhao").val());
                form_data.append("url", $("#add_url").val());
                form_data.append("title", $("#add_head").val());
                form_data.append("token", get_cookie("key"));
                $.ajax({
                    url: ip + "index.php?s=admincms/silde/uploads",
                    data: form_data,
                    type: "post",
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        if (res.resStatus === 0) {
                            $(".loading").fadeOut();
                            $("#lunbo_add").fadeOut();
                            houtai.shop_list_ajax();
                            houtai.hint("<p class='text-success'>添加成功了！</p>");
                        } else{
                            houtai.hint("添加失败了！");
                            $(".loading").fadeOut(200);
                        }
                    },
                    error: function () {
                        $(".loading").fadeOut(200);
                        houtai.hint("<p>添加失败了！</p>");
                    },
                    processData: false,  // 告诉jQuery不要去处理发送的数据
                    contentType: false   // 告诉jQuery不要去设置Content-Type请求头
                });
            }
        },
        xiugai: function (n) {
            if (this.jiancha(n)) {
                $(".loading").fadeIn(200);
                var form_data = new FormData();
                form_data.append("silde_img", $("#input_file_main_set")[0].files[0]);
                form_data.append("sort", $("#set_xuhao").val());
                form_data.append("url", $("#set_url").val());
                form_data.append("title", $("#set_head").val());
                form_data.append("id", houtai.linshi_id);
                form_data.append("token", get_cookie("key"));
                form_data.append("tp", "alter");
                $.ajax({
                    url: ip + "index.php?s=admincms/silde/opto",
                    data: form_data,
                    type: "post",
                    success: function (_res) {
                        var res = JSON.parse(_res);
                        if (res.resStatus === 0) {
                            $(".loading").fadeOut();
                            $("#lunbo_set").fadeOut();
                            houtai.hint("<p class='text-success'>修改成功了！</p>");
                            houtai.shop_list_ajax();
                        } else if (res.resStatus === 1) {
                            houtai.hint("您并没做任何修改啊！");
                            $(".loading").fadeOut(200);
                        }
                    },
                    error: function () {
                        $(".loading").fadeOut(200);
                        houtai.hint("<p>修改失败了！</p>");
                    },
                    processData: false,  // 告诉jQuery不要去处理发送的数据
                    contentType: false   // 告诉jQuery不要去设置Content-Type请求头
                });
            }
        },
        shanchu: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/silde/opto",
                type: "post",
                data: {
                    tp: "del",
                    id: houtai.linshi_id,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        $("#modal_wrap").fadeOut(200);
                        houtai.hint("<p style='color:#0f0;'>删除" + houtai.linshi_id + "成功了</p>");
                        houtai.shop_list_ajax();
                    }
                }
            });
        }
    };

    // 请求数据
    houtai.shop_list_ajax();
    // 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });

    // 添加分类按钮
    $("#jianshe_add_btn").bind("click", function () {
        $("#fenlei_add").fadeIn();
    });
    // 莫态框取消modal
    $("#modal_close").bind("click", function () {
        $("#modal_wrap").fadeOut(200);

    });
    // 修改取消
    $(".cancle_set").bind("click", function () {
        $(".set_shangpin").fadeOut(200);
    });
    // 分类修改
    $("#fenlei_table").on("click", ".fenlei_set", function () {
        houtai.linshi_id = $(this).parents("tr").find(".fenlei_id").text();
        $("#set_name").val($(this).parents("tr").find(".fenlei_name").text());
        $("#set_jieshao").val($(this).parents("tr").find(".fenlei_jieshao").text());
        $("#fenlei_set").fadeIn();
    });
    // 分类删除
    $("#fenlei_table").on("click", ".fenlei_remove", function () {
        houtai.linshi_id = $(this).parents("tr").find(".lunbo_id").text();
        $(".modal-body").empty().append("您确定要删除<span style='color:#f10;'>" + houtai.linshi_id + "</span>吗？");
        $("#modal_wrap").fadeIn();
    });
    // 案例查看
    $("#fenlei_table").on("click", ".anli_chakan", function () {
        houtai.linshi_id = $(this).parents("tr").find(".lunbo_id").text();
        houtai.anli_list_ajax();
        $("#anli_wrap").fadeIn();
    });
    // 确认删除
    $("#confirm").bind("click", function () {
        houtai.shanchu();
    });
    $("#add").bind("click", function () {
        if (houtai.click_yes()) {
            houtai.tianjia(1);
        }
    });
    $("#set").bind("click", function () {
        if (houtai.click_yes()) {
            houtai.xiugai(2);
        }
    });

    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });
});
